package com.example.springboot01.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springboot01.entity.Course;
import com.example.springboot01.entity.ProfessorCourse;
import com.example.springboot01.entity.Teacher;
import com.example.springboot01.mapper.CourseMapper;
import com.example.springboot01.req.PageQueryReq;
import com.example.springboot01.resp.CourseDTO;
import com.example.springboot01.service.CourseService;
import com.example.springboot01.service.ProfessorCourseService;
import com.example.springboot01.service.ProfessorService;
import com.example.springboot01.service.TeacherService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Resource
    private TeacherService teacherService;

    @Resource
    private ProfessorService professorService;

    @Resource
    private ProfessorCourseService psc;




    @Override
    public void addCourse(Course c) {
        Teacher teacher = teacherService.getById(c.getTeacherId());
        if (teacher == null) {
            // throw exception
        }
        this.save(c);
    }

    @Override
    public void delete(Integer cID) {
        this.removeById(cID);
    }

    @Override
    public IPage<CourseDTO> getPage(PageQueryReq pageQueryReq) {
        Page<Course> page = this.page(new Page<Course>(pageQueryReq.getCurrent(), pageQueryReq.getSize()), new LambdaQueryWrapper<>());

        Page<CourseDTO> response = new Page<>();
        BeanUtil.copyProperties(page, response);
        List<CourseDTO> list = new ArrayList<>();
        for (Course record : page.getRecords()) {
            Teacher teacher = teacherService.getById(record.getTeacherId());
            CourseDTO courseDTO = BeanUtil.copyProperties(record, CourseDTO.class);
            if (teacher != null) {
                courseDTO.setTeacherNo(teacher.getTeacherNo());
                courseDTO.setTeacherName(teacher.getTeacherNo());
                courseDTO.setTeacherId(record.getTeacherId());
            }
            list.add(courseDTO);
        }
        response.setRecords(list);

        return response;
    }

    @Override
    public void updateCourse(Course c) {
        Teacher teacher = teacherService.getById(c.getTeacherId());
        if (teacher == null) {
            // throw exception
        }
        this.updateById(c);
    }

    @Override
    public void updateTeacher(Integer cID, Integer tID) {
        Teacher teacher = teacherService.getById(tID);
        if (teacher == null) {
            // throw exception
        }
        Course course = this.getById(cID);
        course.setTeacherId(tID);
        this.updateById(course);
    }

    @Override
    public Course getByTeacher(Integer teacherId) {
        return this.getOne(new LambdaQueryWrapper<Course>()
                .eq(Course::getTeacherId, teacherId)
        );
    }

    @Override
    public List<Course> getByProfessor(Integer professorId) {
        final List<ProfessorCourse> list = psc.list(new LambdaQueryWrapper<ProfessorCourse>()
                .eq(ProfessorCourse::getProfessorId, professorId)
        );
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }
        return this.listByIds(list.stream().map(ProfessorCourse::getCourseId).collect(Collectors.toList()));
    }

}
